package scriptrunner.execution;

import java.io.Serializable;

public class ValueIntegerObject extends ValueObject implements Serializable,
		Cloneable, ArithmeticCalculable, ComparableValue {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6220150364577023170L;

	public ValueIntegerObject() {
		this.value = 0;
	}

	public ValueIntegerObject(int v) {
		this.value = v;
	}

	@Override
	public ValueIntegerObject clone() throws CloneNotSupportedException {
		ValueIntegerObject o = (ValueIntegerObject) super.clone();
		o.value = value;
		return o;
	}

	public void SetValue(int v) {
		value = v;
	}

	public int GetValue() {
		return value;
	}

	int value;

	@Override
	public ValueIntegerObject Add(Addable o) throws ExecuteErrorException {
		if (o.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject result = new ValueIntegerObject();
		result.value = value + ((ValueIntegerObject) o).value;
		return result;
	}

	@Override
	public ValueIntegerObject Minus(ArithmeticCalculable o)
			throws ExecuteErrorException {
		if (o.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject result = new ValueIntegerObject();
		result.value = value - ((ValueIntegerObject) o).value;
		return result;
	}

	@Override
	public ValueIntegerObject Times(ArithmeticCalculable o)
			throws ExecuteErrorException {
		if (o.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject result = new ValueIntegerObject();
		result.value = value * ((ValueIntegerObject) o).value;
		return result;
	}

	@Override
	public ValueIntegerObject Divide(ArithmeticCalculable o)
			throws ExecuteErrorException {
		if (o.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject result = new ValueIntegerObject();
		result.value = value / ((ValueIntegerObject) o).value;
		return result;
	}

	@Override
	public ValueIntegerObject UnaryMinus() throws ExecuteErrorException {
		ValueIntegerObject result = new ValueIntegerObject();
		result.value = -value;
		return result;
	}

	@Override
	public void ParseFromString(String s) throws ExecuteErrorException {
		try {
			value = Integer.parseInt(s);
		} catch (Throwable e) {
			throw (new ExecuteErrorException("Cannot convert the value to an Integer"));
		}
	}

	@Override
	public int ArithmeticTypePreference() {
		return 0;
	}

	@Override
	public String SaveValueToString() {
		return Integer.toString(value);
	}

	@Override
	public int AddTypePerference() {
		return 0;
	}

	@Override
	public boolean LessThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject rhsValue = (ValueIntegerObject) rhs;

		return value < rhsValue.value;
	}

	@Override
	public boolean GreaterThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject rhsValue = (ValueIntegerObject) rhs;

		return value > rhsValue.value;
	}

	@Override
	public boolean LessEqThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject rhsValue = (ValueIntegerObject) rhs;

		return value <= rhsValue.value;
	}

	@Override
	public boolean GreaterEqThan(ComparableValue rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject rhsValue = (ValueIntegerObject) rhs;

		return value >= rhsValue.value;
	}

	@Override
	public boolean EqualTo(EqualAndNotEqual rhs) throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject rhsValue = (ValueIntegerObject) rhs;

		return value == rhsValue.value;
	}

	@Override
	public boolean NotEqualTo(EqualAndNotEqual rhs)
			throws ExecuteErrorException {
		if (rhs.getClass() != this.getClass())
			throw (new ExecuteErrorException("The value types do not match"));

		ValueIntegerObject rhsValue = (ValueIntegerObject) rhs;

		return value != rhsValue.value;
	}
	
	@Override
	public boolean equals(Object o) {
		if ( o == null || o.getClass() != getClass())
			return false;
		ValueIntegerObject rhs = (ValueIntegerObject)o;
		return value == rhs.value;
	}
	
	@Override
	public String toString() {
		return Integer.toString(value);
	}
	
}
